package org.jeecg.modules.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.app.constant.AppConstant;
import org.jeecg.modules.app.entity.AppClient;
import org.jeecg.modules.app.entity.AppClientMenu;
import org.jeecg.modules.app.entity.AppMenu;
import org.jeecg.modules.app.entity.AppUpgrade;
import org.jeecg.modules.app.mapper.AppClientMapper;
import org.jeecg.modules.app.mapper.AppClientMenuMapper;
import org.jeecg.modules.app.mapper.AppMenuMapper;
import org.jeecg.modules.app.mapper.AppUpgradeMapper;
import org.jeecg.modules.app.service.IAppClientMenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 设置菜单
 * @Author: sean
 * @Date:   2024-10-25
 * @Version: V1.0
 */
@RequiredArgsConstructor
@Service
public class AppClientMenuServiceImpl extends ServiceImpl<AppClientMenuMapper, AppClientMenu> implements IAppClientMenuService {

    private final AppClientMapper appClientMapper;
    private final AppUpgradeMapper appUpgradeMapper;
    private final AppMenuMapper appMenuMapper;
    private final RedisUtil redisUtil;
    @Override
    public IPage<AppClientMenu> selectJoinPage(Page page, AppClientMenu appClientMenu) {
        return baseMapper.selectJoinPage(page, appClientMenu);
    }

    @Override
    public void saveClientMenu(String clientId, String menuIds) {
        //删除旧的客户端菜单
        baseMapper.delete(new QueryWrapper<AppClientMenu>().eq("client_id", clientId));
        //删除客户端菜单缓存
        List<AppUpgrade> upgradeList = appUpgradeMapper
                .selectList(new QueryWrapper<AppUpgrade>().eq("client_id", clientId));
        upgradeList.forEach(item->{
            redisUtil.del(AppConstant.APP_CLIENT_MENUS_CACHE_KEY + "::" + item.getClientId());
        });
        //保存新的客户端菜单
        List<AppClientMenu> list = Arrays.stream(menuIds.split(",")).map(a-> {
            AppClientMenu clientMenu = new AppClientMenu();
            clientMenu.setClientId(Long.parseLong(clientId));
            clientMenu.setMenuId(Long.parseLong(a));
            return clientMenu;
        }).collect(Collectors.toList());
        this.saveBatch(list);
    }

    @Override
    public List<String> getMenuCode(String orgCode, String platform) {
        AppClient client = appClientMapper.selectOne(new QueryWrapper<AppClient>().eq("sys_org_code", orgCode).eq("app_category", platform));
        if(ObjectUtil.isNotEmpty(client)){
            List<AppClientMenu> list = baseMapper.selectList(new QueryWrapper<AppClientMenu>().lambda().eq(AppClientMenu::getClientId, client.getId()));
            Set<Long> menuIdList = list.stream().map(AppClientMenu::getMenuId).collect(Collectors.toSet());
            List<AppMenu> menuList = appMenuMapper.selectBatchIds(menuIdList);
            return menuList.stream().map(AppMenu::getCode).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
